<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mailings extends MY_Controller {

	public function index(){
		$this->load->view('mailings');
	}

	/*
	 * get mailings
	 */
	public function getMailings(){
		$results = $this->mailing->getMailings();

		$arrayResults = '';
		foreach ($results as $key => $row){
			$arrayResults[$key]['text'] = $row->name;
			$arrayResults[$key]['id'] = $row->id_node;
			$arrayResults[$key]['leaf'] = $row->is_mailing;
		}

		echo json_encode($arrayResults);
	}

	/*
	 * save mailings
	 */
	public function saveMailings(){
		$results = $this->mailing->saveMailing();
		echo json_encode($results);
	}

	/*
	 * update details
	 */
	public function updateDetails(){
		$results = $this->mailing->updateDetails();
		if($results){$code = 200;}
		else{$code == 400;}
		$return['code'] = $code;
		echo json_encode($return);
	}

	/*
	 * update content
	 */
	public function updateContent(){
		$results = $this->mailing->updateContent();
		if($results){
			$code = 200;
		}
		else{
			$code == 400;
		}
		$return['code'] = $code;
		echo json_encode($return);
	}

	/*
	 * get mailing details
	 */
	public function getDetails(){
		$results = $this->mailing->getDetails();
		echo json_encode($results);
	}
	
	/*
	 * remove mailing
	 */
	public function removeMailing(){
		$results = $this->mailing->removeMailing();
		echo json_encode($results);
	}

	/*
	 * test mailing
	 */
	public function testMailing(){
		$this->load->library('email');

		//get details
		$results = $this->mailing->getDetails();

		//get params
		$params = json_decode($_POST['params']);

		//set config
		/*
		$config['protocol'] = 'smtp';
		$config['smtp_host'] = 'ssl://smtp.gmail.com';
		$config['smtp_port'] = '465';
		$config['smtp_timeout'] = '7';
		$config['smtp_user'] = 'qlstiffler@gmail.com';
		$config['smtp_pass'] = 'Q757243L_l';
		*/
		$config['protocol'] = 'sendmail';	//necessary in nazwa.pl
		$config['charset'] = 'utf-8';
		$config['newline'] = "\r\n";
		$config['mailtype'] = 'html';
		$config['validation'] = false;

		$this->email->initialize($config);

		$this->email->from($results[0]->sender_email, $results[0]->sender_name);
		$this->email->to($params->test_recipients);

		$this->email->subject($results[0]->email_title);
		$this->email->message($results[0]->email_content);

		if($this->email->send()){
			$code = 200;
		}
		else{
			$code = 400;
		}
		$return['code'] = $code;
		echo json_encode($return);
	}

	/*
	 * get recipients
	 */
	public function getRecipients(){
		$return['results'] = $this->mailing->getRecipients();
		$return['count'] = $this->mailing->countRecipients();
		echo json_encode($return);
	}
	
	/*
	 * send mailing
	 */
	public function sendMailing(){
		$this->load->library('email');
		
		//get details
		$details = $this->mailing->getDetails();
		//set config
		/*
		$config['protocol'] = 'smtp';
		$config['smtp_host'] = 'ssl://smtp.gmail.com';
		$config['smtp_port'] = '465';
		$config['smtp_timeout'] = '7';
		$config['smtp_user'] = 'qlstiffler@gmail.com';
		$config['smtp_pass'] = 'Q757243L_l';
		*/
		$config['protocol'] = 'sendmail';	//necessary in nazwa.pl
		$config['charset'] = 'utf-8';
		$config['newline'] = "\r\n";
		$config['mailtype'] = 'html';
		$config['validation'] = false;
		
		for($i=0; $i<10; $i++){
			//get recipients to send
			$recipient = $this->mailing->getRecipientToSend();
			if(isset($recipient[0])){
				$this->email->initialize($config);
				$this->email->from($details[0]->sender_email, $details[0]->sender_name);
				$this->email->to($recipient[0]->email);
				$this->email->subject($details[0]->email_title);
				$this->email->message($details[0]->email_content);
				if($this->email->send()){
					$this->mailing->updateRecipient($recipient[0]);
				}
			}
			else{
				break;
			}
		}
		
		$return['code'] = 200;
		$return['countAll'] = $this->mailing->countRecipients();
		$return['countSent'] = $this->mailing->countSentRecipients();
		echo json_encode($return);
		
		
	}

	/*
	 * save recipients
	 */
	public function saveRecipients(){
		$this->load->library('csvreader');
		$results = $this->mailing->saveRecipients();
		if($results){
			$code = 200;
		}
		else{
			$code == 400;
		}
		$return['code'] = $code;
		echo json_encode($return);
	}
	
	/*
	 * upload files
	 */
	public function uploadFiles(){
		$options = array(
			'upload_dir' => dirname(__FILE__).'/../../assets/mailings/',
			'upload_url' => dirname($_SERVER['PHP_SELF']).'/../../assets/mailings/',
			'param_name' => 'files',
			'thumbnail' => array(
				'upload_dir' => dirname(__FILE__).'/../../assets/mailings/thumbnails/',
				'upload_url' => dirname($_SERVER['PHP_SELF']).'/../../assets/mailings/thumbnails/',
				'max_width' => 80,
				'max_height' => 80
			)
		);
		$this->load->library('uploadhandler', $options);
		
		header('Pragma: no-cache');
		header('Cache-Control: private, no-cache');
		header('Content-Disposition: inline; filename="files.json"');
		header('X-Content-Type-Options: nosniff');
		switch ($_SERVER['REQUEST_METHOD']) {
			case 'HEAD':
			case 'GET':
				$this->uploadhandler->get();
				break;
			case 'POST':
				$this->uploadhandler->post();
				break;
			case 'DELETE':
				$this->uploadhandler->delete();
				break;
			case 'OPTIONS':
				break;
			default:
				header('HTTP/1.0 405 Method Not Allowed');
		}
	}
}